package com.zh.web.base;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;


@Api("响应结果")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult<T> {
    @ApiModelProperty(value = "响应数据")
    private T data;
    @ApiModelProperty(value = "返回信息")
    private String msg;
    @ApiModelProperty(value = "状态码")
    private int code;

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.data = data;
        apiResult.msg = "success";
        apiResult.code = 200;
        return apiResult;
    }

    public static <T> ApiResult<T> msg(T msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.data = null;
        apiResult.msg = (String) msg;
        apiResult.code = 200;
        return apiResult;
    }

    public static <T> ApiResult<T> ok() {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.msg = "success";
        apiResult.code = 200;
        apiResult.data = null;
        return apiResult;
    }

    public static <T> ApiResult<T> fail(String msg, int code) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.data = null;
        apiResult.msg = msg;
        apiResult.code = code;
        return apiResult;
    }

    public static <T> ApiResult<T> fail(String msg) {
        return fail(msg, 500);
    }

}
